import React from 'react'
import Tit from '../../components/Tit'
import { withRouter } from 'react-router-dom'
import { TextareaItem } from 'antd-mobile'
import './Evaluation.less'
class Evaluation extends React.Component {
    state = {
        Evaluation: null,
    }
    handleChange(index, key) {
        this.state.Evaluation[index].params = key
        this.setState({ Evaluation: this.state.Evaluation })
    }
    componentDidMount() {
        console.log(this.props.match.params.id)
        this.$postData('/api/order/getOrderMsg', { orderId: this.props.match.params.id }, params => {
            console.log(params)
            switch (params.code) {
                case 100:
                    params.data.voList.map((val) => {
                        val.xing = [true, true, true, true, true];
                        val.params = ''
                    })
                    this.setState({
                        Evaluation: params.data.voList
                    })
                    console.log(params.data.voList)
                    break;
                default:
                    this.Toast.info(params.msg);
                    break;
            }
        })
    }
    pingjia(index1, index2) {
        console.log(index1, index2)
        for (var i = 0; i < this.state.Evaluation[index1].xing.length; i++) {
            if (i <= index2) {
                this.state.Evaluation[index1].xing[i] = true
            } else {
                this.state.Evaluation[index1].xing[i] = false
            }
        }
        this.setState({
            Evaluation: this.state.Evaluation
        })
        var a = 0
        this.state.Evaluation[index1].xing.map((val) => {
            if (val) { a += 1 }
        })
        console.log(a)
    }
    assessOrder() {
        console.log(this.state.Evaluation)
        var params = []
        this.state.Evaluation.map((val) => {
            var a = {}
            a.content = val.params
            for (var i = 0; i <= 4; i++) {
                console.log(val.xing)
                if (val.xing[i] == false) {
                    console.log(i);
                    a.starNum = i;
                    i = 5
                }
                else if (i == 4 && val.xing[i]) {
                    console.log(i)
                    a.starNum = 5
                }
            }
            a.id = val.id
            params.push(a)
        })
        this.$postData('/api/order/assessOrder?orderId=' + this.props.match.params.id, params, params => {
            console.log(params)
            switch (params.code) {
                case 100:
                    this.Toast.info(params.msg);
                    this.props.history.goBack()
                    break;
                default:
                    this.Toast.info(params.msg);
                    break;
            }
        }, true)
    }
    render() {
        return (
            <div><Tit zz={true} tit={'评价'}></Tit>
                <p className='Evaluation-p1' onClick={this.assessOrder.bind(this)}>发布</p>
                {this.state.Evaluation ? this.state.Evaluation.map((val, index1) => (
                    <div key={index1} className='Evaluation-d1'>
                        <div className='Evaluation-d2'>
                            <img className='Evaluation-i1' src={val.picUrl} alt="" />
                            <div className='Evaluation-d3'>
                                {val.xing.map((val, index2) => (
                                    <img key={index2} onClick={this.pingjia.bind(this, index1, index2)} src={val ? require('../../static/img/xing-quan.png') : require('../../static/img/xing-kong.png')} alt="" />
                                ))}
                            </div>
                        </div>
                        <div className='AfterS-d3'>
                            <TextareaItem
                                rows={3}
                                placeholder='宝贝满足你的期待吗？说说你的体验吧'
                                maxLength='50'
                                value={val.params}
                                onChange={this.handleChange.bind(this, index1)}
                            />
                        </div>
                    </div>
                ))
                    : ''}
            </div>
        )
    }
}
export default withRouter(Evaluation)